#include "newsgroup.h"
#include <iostream>

using namespace std;


static unsigned int id_number = 0;

/* in the case of a disc based database we wish to resume our count
 from the last session */

void set_begin_id_number(unsigned int& number)
{
	id_number = number;
}

unsigned int get_id_number(void) 
{
	return id_number;	
}


NewsGroup::NewsGroup(string& n_name): name(n_name), art_id(0) {
	++id_number;		
	id = id_number;		
}

NewsGroup::NewsGroup(string& n_name, unsigned int& id_num, unsigned int& idart): 
		id(id_num), name(n_name), art_id(idart) { }

NewsGroup::~NewsGroup()
{
	map<unsigned int, Article*>::iterator itr;

	for (itr = articles.begin(); itr != articles.end(); ++itr)
		delete itr->second;
		
}

unsigned int NewsGroup::get_id()
{
	return id;	
}

unsigned int NewsGroup::get_art_id_count(void)
{
	return art_id;
}

string NewsGroup::get_title()
{
	return name;
}

bool NewsGroup::add_article(string& title, string& text, string& author) 
{
	++art_id;	
	articles[art_id] = new Article(title, text, author);	
	cout << "ART ID of " << title << " is: " << art_id;
	return true;
}

void NewsGroup::add_article(unsigned int& art_id, string& title, string& text, string& author) 
{
	articles[art_id] = new Article(title, text, author);	
}

bool NewsGroup::get_article(Article** dest, unsigned int& art_id)
{
	map<unsigned int, Article*>::iterator itr;	
	if ((itr = articles.find(art_id)) == articles.end())	
		return false;
	*dest = itr->second;	

	return true;	
}

bool NewsGroup::delete_article(unsigned int& art_id)
{
	map<unsigned int, Article*>::iterator iter;

	iter = articles.find(art_id);
	if (iter == articles.end())
		return false;

	delete iter->second;	
	articles.erase(iter);

	return true;
}	
int NewsGroup::size()
{
	return articles.size();
}
map<unsigned int, Article*>::iterator  NewsGroup::begin()
{
	return articles.begin();		
}
map<unsigned int, Article*>::iterator  NewsGroup::end()
{
	return articles.end();		
}


Article::Article(string& art_title, string& art_text, string& author): 
		title(art_title), author(author) , text(art_text)
{ 
}
